package binaryOctopus;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 * The Board class contains the data required to create and display a graphic
 * representation of the board, using Java Swing.
 */
public class Board extends JPanel {

	// number of rows and cols board has
	private final int numRows, numCols;
	// array of squares that make up the checker board
	private JButton[][] squares;
	// a layout that will automatically center the image of the piece on square
	private GridLayout gbag = new GridLayout(8, 8);

	private boolean isP1Turn;

	public PieceListener pListener = new PieceListener(this);
	public CheckersGui myGui;
	
	private int numReds;
	private int numBlacks;

	/**
	 * Creates graphics of a new checkers board to a specified size
	 * 
	 * @param numRows
	 *            number of squares in each row
	 * @param numCols
	 *            number of squares in each column
	 */
	public Board(int numRows, int numCols, CheckersGui g) {
		this.numRows = numRows;
		this.numCols = numCols;
		squares = new JButton[numRows][numCols];
		setLayout(gbag);
		initializeBoard();
		myGui = g;
		isP1Turn = true;
	
		numReds = 12;
		numBlacks = 12;
	}

	/**
	 * Lets the player move a piece on the board that must be valid by standard
	 * game rules.
	 * 
	 * @param original
	 * @param newCoordinate
	 */
	public void move(Coordinates original, Coordinates newCoordinate) {
		// will check to make sure move is valid and call move again
		// until a valid move is given
		if (isValidMove(original, newCoordinate)) {
			myGui.movePieces(original, newCoordinate);
			kingPiece(newCoordinate);
			isP1Turn = !isP1Turn;
		}
	}

	/**
	 * Checks to see if the coordinates given are valid in the standard rules of
	 * checkers
	 * 
	 * @param c1
	 *            the coordinate of the first piece
	 * @param c2
	 *            the coordinate of the second piece
	 * @return true if a move is valid
	 */
	private boolean isValidMove(Coordinates c1, Coordinates c2) 
	{
		// Checks to make sure that the first coordinate has a piece and the
		// second doesn't
		if (squares[c1.getRow()][c1.getColumn()].getIcon() != null
				&& squares[c2.getRow()][c2.getColumn()].getIcon() == null) 
		{
			// Checks to make sure that it is player 1's turn (black)
			if (isP1Turn
					&& squares[c1.getRow()][c1.getColumn()].getIcon()
							.toString().equals("blackPiece.gif")) 
			{
				if (c2.getRow() == c1.getRow() + 1
						&& c2.getColumn() == c1.getColumn() + 1 
						&& !forceJump()) 
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() + 1
						&& c2.getColumn() == c1.getColumn() - 1 
						&& !forceJump()) 
				{
					return true;
				}
				if (jumpPiece(c1, c2, false)) 
				{
					
					return true;
				}
			}
			// Checks to make sure that it is a king (black)
			if (isP1Turn
					&& squares[c1.getRow()][c1.getColumn()].getIcon()
							.toString().equals("blackKing.gif")) 
			{
				/*int rows_change = c1.getRow() - c2.getRow();

				int cordinateRowDiff = c1.getRow() - c2.getRow();
				int cordinateColDiff = c1.getColumn() - c2.getColumn();						 
				int coordinateRowChange = 0;
				int coordinateColChange = 0;

				if (cordinateRowDiff > 0) {
					coordinateRowChange = 1;
				} else {
					coordinateRowChange = -1;
				}

				if (cordinateColDiff > 0) {
					coordinateColChange = 1;
				} else {
					coordinateColChange = -1;
				}
				
				if (c2.getRow() != c1.getRow() && !forceJump()
						&& (c2.getColumn() == c1.getColumn() + rows_change*coordinateRowChange
						|| c2.getColumn() == c1.getColumn() + rows_change*coordinateColChange)) {
					return true;
				}*/
					

				if (c2.getRow() == c1.getRow() + 1
						&& c2.getColumn() == c1.getColumn() + 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() + 1
						&& c2.getColumn() == c1.getColumn() - 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() - 1
						&& c2.getColumn() == c1.getColumn() + 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() - 1
						&& c2.getColumn() == c1.getColumn() - 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (jumpPiece(c1, c2, true) ) 
				{
					return true;
				}
				System.out.println("did not return true");
			}
			// Checks to make sure that it is player 2's turn (red)
			if (!isP1Turn
					&& squares[c1.getRow()][c1.getColumn()].getIcon()
							.toString().equals("redPiece.gif")) 
			{
				if (c2.getRow() == c1.getRow() - 1
						&& c2.getColumn() == c1.getColumn() + 1  
						&& !forceJump()
						)
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() - 1
						&& c2.getColumn() == c1.getColumn() - 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (jumpPiece(c1, c2, false)) 
				{
					return true;
				}
			}
			// Checks to make sure that it is a king (red)
			if (!isP1Turn
					&& squares[c1.getRow()][c1.getColumn()].getIcon()
							.toString().equals("redKing.gif")) 
			{
				if (c2.getRow() == c1.getRow() + 1
						&& c2.getColumn() == c1.getColumn() + 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() + 1
						&& c2.getColumn() == c1.getColumn() - 1   
						&& !forceJump()
						) 
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() - 1
						&& c2.getColumn() == c1.getColumn() + 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (c2.getRow() == c1.getRow() - 1
						&& c2.getColumn() == c1.getColumn() - 1  
						&& !forceJump()
						) 
				{
					return true;
				}
				if (jumpPiece(c1, c2, true) 
						
						) 
				{
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Checks the coordinate to see if there is a jump to be made and removes
	 * the piece jumped if there is
	 * 
	 * @param c1
	 *            the coordinate of the first piece
	 * @param c2
	 *            the coordinate of the second piece
	 * @param isKing
	 *            true if it is a king
	 * @return if the piece should be jumped
	 */
	private boolean jumpPiece(Coordinates c1, Coordinates c2, boolean isKing) {
		if (isP1Turn && !isKing) {
			if (c2.getRow() == c1.getRow() + 2
					&& c2.getColumn() == c1.getColumn() + 2
					&& canJump(c1, c2, false)) 
			{
				myGui.removePiece(new Coordinates(c1.getRow() + 1, c1
						.getColumn() + 1));
				
					numReds--;
					
					System.out.println("1REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
				
			} 
			else if (c2.getRow() == c1.getRow() + 2
					&& c2.getColumn() == c1.getColumn() - 2
					&& canJump(c1, c2, false)) 
			{
				myGui.removePiece(new Coordinates(c1.getRow() + 1, c1
						.getColumn() - 1));
				
					numReds--;
					
					System.out.println("2REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
				
			}
		}
		if (!isP1Turn && !isKing) {
			if (c2.getRow() == c1.getRow() - 2
					&& c2.getColumn() == c1.getColumn() + 2
					&& canJump(c1, c2, false)) {
				myGui.removePiece(new Coordinates(c1.getRow() - 1, c1
						.getColumn() + 1));
				
					numBlacks--;
					System.out.println("3REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
				
			} else if (c2.getRow() == c1.getRow() - 2
					&& c2.getColumn() == c1.getColumn() - 2
					&& canJump(c1, c2, false)) {
				myGui.removePiece(new Coordinates(c1.getRow() - 1, c1
						.getColumn() - 1));
				
					numBlacks--;
					System.out.println("4REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
			
				
			}
		}
		if (isKing) {
			int rows_change = c1.getRow() - c2.getRow();

			int cordinateRowDiff = c1.getRow() - c2.getRow();
			int cordinateColDiff = c1.getColumn() - c2.getColumn();						 
			int coordinateRowChange = 0;
			int coordinateColChange = 0;

			if (cordinateRowDiff > 0) {
				coordinateRowChange = 1;
			} else {
				coordinateRowChange = -1;
			}

			if (cordinateColDiff > 0) {
				coordinateColChange = 1;
			} else {
				coordinateColChange = -1;
			}

			//System.out.println(rows_change);
			//System.out.println(coordinateRowChange);
			//System.out.println(coordinateColChange);
			 
			if (c2.getRow() != c1.getRow() 
					&& (c2.getColumn() == c1.getColumn() + rows_change*coordinateRowChange
					|| c2.getColumn() == c1.getColumn() + rows_change*coordinateColChange)) // && canJump(c1, c2, true) 
			{
				Coordinates opponentPiece = new Coordinates(c2.getRow() + 
						coordinateRowChange, c2.getColumn() + coordinateColChange);
					if(oppositeSides(opponentPiece,c1)) {
					myGui.removePiece(new Coordinates(c2.getRow() + coordinateRowChange, 
							c2.getColumn() + coordinateColChange));
					
						numBlacks--;
						System.out.println("5REDS:" + numReds + " BLACKS: " + numBlacks);
						return true;
				}
				
			}
			/*if (c2.getRow() == c1.getRow() + 2
					&& c2.getColumn() == c1.getColumn() + 2 && canJump(c1, c2, true)) 
			{
				myGui.removePiece(new Coordinates(c1.getRow() + 1, c1
						.getColumn() + 1));
				
					numBlacks--;
					System.out.println("5REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
				
			} 
			else if (c2.getRow() == c1.getRow() + 2
					&& c2.getColumn() == c1.getColumn() - 2 && canJump(c1, c2, true)) 
			{
				myGui.removePiece(new Coordinates(c1.getRow() + 1, c1
						.getColumn() - 1));
				
					numBlacks--;
					System.out.println("6REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
				
			}
			if (c2.getRow() == c1.getRow() - 2
					&& c2.getColumn() == c1.getColumn() + 2 && canJump(c1, c2, true)) 
			{
				myGui.removePiece(new Coordinates(c1.getRow() - 1, c1
						.getColumn() + 1));
				
					numReds--;
					System.out.println("7REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
				
			} 
			else if (c2.getRow() == c1.getRow() - 2
					&& c2.getColumn() == c1.getColumn() - 2 && canJump(c1, c2, true)) 
			{
				myGui.removePiece(new Coordinates(c1.getRow() - 1, c1
						.getColumn() - 1));
				
					numReds--;
					System.out.println("8REDS:" + numReds + " BLACKS: " + numBlacks);
					return true;
				
			}*/
		}
		return false;
	}

	/*
	 * This fixed an error where a player can jump over and remove his own
	 * piece. Also fixed error where a player could jump over an empty square at
	 * any point in time.
	 */
	private boolean canJump(Coordinates c1, Coordinates c2, boolean isKing) 
	{
		if (!isKing) 
		{
			if (isP1Turn) 
			{
				if (c2.getRow() == c1.getRow() + 2 && c2.getColumn() == c1.getColumn() + 2) 
				{
					if (squares[c1.getRow() + 1][c1.getColumn() + 1].getIcon().toString().equals("redPiece.gif")
							|| squares[c1.getRow() + 1][c1.getColumn() + 1].getIcon().toString().equals("redKing.gif")) 
					{
						return true;
					}
				} 
				else if (c2.getRow() == c1.getRow() + 2 && c2.getColumn() == c1.getColumn() - 2) 
				{
					if (squares[c1.getRow() + 1][c1.getColumn() - 1].getIcon().toString().equals("redPiece.gif")
							|| squares[c1.getRow() + 1][c1.getColumn() - 1].getIcon().toString().equals("redKing.gif")) 
					{
						return true;
					}
				}
			}
			if (!isP1Turn) {
				if (c2.getRow() == c1.getRow() - 2
						&& c2.getColumn() == c1.getColumn() + 2) {
					if (squares[c1.getRow() - 1][c1.getColumn() + 1].getIcon()
							.toString().equals("blackPiece.gif")
							|| squares[c1.getRow() - 1][c1.getColumn() + 1]
									.getIcon().toString()
									.equals("blackKing.gif")) {
						return true;
					}
				} else if (c2.getRow() == c1.getRow() - 2
						&& c2.getColumn() == c1.getColumn() - 2) {
					if (squares[c1.getRow() - 1][c1.getColumn() - 1].getIcon()
							.toString().equals("blackPiece.gif")
							|| squares[c1.getRow() - 1][c1.getColumn() - 1]
									.getIcon().toString()
									.equals("blackKing.gif")) {
						return true;
					}

				}
			}
		}
		if (isKing) 
		{
			if (isP1Turn) 
			{
				if (c1.getRow() + 2 < 8 && c1.getColumn() + 2 < 8 && c2.getRow() == c1.getRow() + 2
						&& c2.getColumn() == c1.getColumn() + 2) {
					if (squares[c1.getRow() + 1][c1.getColumn() + 1].getIcon()
							.toString().equals("redPiece.gif")
							|| squares[c1.getRow() + 1][c1.getColumn() + 1]
									.getIcon().toString().equals("redKing.gif")) 
					{
						return true;
					}
				}
				else if (c1.getRow() + 2 < 8 && c1.getColumn() - 2 >= 0 && c2.getRow() == c1.getRow() + 2
						&& c2.getColumn() == c1.getColumn() - 2) {
					if (squares[c1.getRow() + 1][c1.getColumn() - 1].getIcon()
							.toString().equals("redPiece.gif")
							|| squares[c1.getRow() + 1][c1.getColumn() - 1]
									.getIcon().toString().equals("redKing.gif")) 
					{
						return true;
					}
				} 
				else if (c1.getRow() - 2 >= 0 && c1.getColumn() + 2 < 8 && c2.getRow() == c1.getRow() - 2
						&& c2.getColumn() == c1.getColumn() + 2)
				{
					if (squares[c1.getRow() - 1][c1.getColumn() + 1].getIcon()
							.toString().equals("redPiece.gif")
							|| squares[c1.getRow() - 1][c1.getColumn() + 1]
									.getIcon().toString().equals("redKing.gif")) 
					{
						return true;
					}
				} 
				else if (c1.getRow() - 2 >= 0 && c1.getColumn() -2 >= 0 && c2.getRow() == c1.getRow() - 2
						&& c2.getColumn() == c1.getColumn() - 2) 
				{
					if (squares[c1.getRow() - 1][c1.getColumn() - 1].getIcon()
							.toString().equals("redPiece.gif")
							|| squares[c1.getRow() - 1][c1.getColumn() - 1]
									.getIcon().toString().equals("redKing.gif")) 
					{
						return true;
					}

				}
			}
			if (!isP1Turn) 
			{
				if (c1.getRow() - 2 >= 0 && c1.getColumn() < 8 && c2.getRow() == c1.getRow() - 2
						&& c2.getColumn() == c1.getColumn() + 2) {
					if (squares[c1.getRow() - 1][c1.getColumn() + 1].getIcon()
							.toString().equals("blackPiece.gif")
							|| squares[c1.getRow() - 1][c1.getColumn() + 1]
									.getIcon().toString()
									.equals("blackKing.gif")) 
					{
						return true;
					}
				} 
				else if (c1.getRow() - 2 >= 0 && c1.getColumn() -2 >= 0 && c2.getRow() == c1.getRow() - 2
						&& c2.getColumn() == c1.getColumn() - 2) 
				{
					if (squares[c1.getRow() - 1][c1.getColumn() - 1].getIcon()
							.toString().equals("blackPiece.gif")
							|| squares[c1.getRow() - 1][c1.getColumn() - 1]
									.getIcon().toString()
									.equals("blackKing.gif")) 
					{
						return true;
					}

				} 
				else if (c1.getRow() + 2 < 8 && c1.getColumn() + 2 < 8 && c2.getRow() == c1.getRow() + 2
						&& c2.getColumn() == c1.getColumn() + 2) 
				{
					if (squares[c1.getRow() + 1][c1.getColumn() + 1].getIcon()
							.toString().equals("blackPiece.gif")
							|| squares[c1.getRow() + 1][c1.getColumn() + 1]
									.getIcon().toString()
									.equals("blackKing.gif")) 
					{
						return true;
					}
				} 
				else if (c1.getRow() + 2 < 8 && c1.getColumn() - 2 >= 0 && c2.getRow() == c1.getRow() + 2
						&& c2.getColumn() == c1.getColumn() - 2) 
				{
					if (squares[c1.getRow() + 1][c1.getColumn() - 1].getIcon()
							.toString().equals("blackPiece.gif")
							|| squares[c1.getRow() + 1][c1.getColumn() - 1]
									.getIcon().toString()
									.equals("blackKing.gif")) {
						return true;
					}
				}
			}
		}
		return false;

	}

	public boolean gameOver()
	{
	    // Checks to see if game is over based on number of checkers left.
	   
		return (numReds == 0 || numBlacks == 0);
	}

	    // Returns color of the winner.
	public boolean blackWin() {
		if (numBlacks == 0)
		    return false;
		else
		    return true;
	}
	
	// Returns true if c1 has a red piece and c2 has a black piece and vice-versa. Returns false otherwise.
	private boolean oppositeSides(Coordinates c1, Coordinates c2) {
		
		if (!checkSquare(c1) || !checkSquare(c2)) {
			return false;
		} else if ((squares[c1.getRow()][c1.getColumn()].getIcon().toString().equals("blackPiece.gif") 
				|| squares[c1.getRow()][c1.getColumn()].getIcon().toString().equals("blackKing.gif")) 
				&& (squares[c2.getRow()][c2.getColumn()].getIcon().toString().equals("redPiece.gif")
				|| squares[c2.getRow()][c2.getColumn()].getIcon().toString().equals("redKing.gif"))) {
			return true;
		} else 	if ((squares[c2.getRow()][c2.getColumn()].getIcon().toString().equals("blackPiece.gif") 
				|| squares[c2.getRow()][c2.getColumn()].getIcon().toString().equals("blackKing.gif")) 
				&& (squares[c1.getRow()][c1.getColumn()].getIcon().toString().equals("redPiece.gif")
				|| squares[c1.getRow()][c1.getColumn()].getIcon().toString().equals("redKing.gif"))) {
			return true;
		}
		
		return false;
	}
	
	 private boolean forceJump()
	 {
		 JButton currentSquare;
		 JButton newSquare;
		 int whichPlayersTurn;
		 String pieceColor1;
		 String pieceColor2;
		 //ArrayList<int> distance = new ArrayList(); 		 
		 
		 if(getWhoTurn()) {
			 whichPlayersTurn = 1;
			 pieceColor1 = "blackPiece.gif";
			 pieceColor2 = "blackKing.gif";
		 } else {
			 whichPlayersTurn = 2;
			 pieceColor1 = "redPiece.gif";
			 pieceColor2 = "redKing.gif";
		 }
		 	 	 
		 // Loop over each piece
		 for(int i = 0; i < 8; i++)
		 {
			 for(int j = 0; j < 8; j++)
			 {
				 
				 currentSquare = squares[i][j];
				 Coordinates currentCoordinate = new Coordinates(i, j);
				 
				 if(currentSquare.getIcon() != null && whichPlayersTurn == 1 
						 && (currentSquare.getIcon().toString().equals(pieceColor1)
						 || currentSquare.getIcon().toString().equals(pieceColor2))) {
					 // Player 1's turn	and current piece is player 1's		 
				 } else if(currentSquare.getIcon() != null && whichPlayersTurn == 2 
						 && (currentSquare.getIcon().toString().equals(pieceColor1)
						 || currentSquare.getIcon().toString().equals(pieceColor2))) {
					 // Player 2's turn and current piece is player 2's	
				 } else {
					 // Skip this piece if it's not the currently active player's piece
					 continue;
				 }
				 
				 
				 // Try to find neighbors of current piece by looping through all pieces 
				 // again for this current piece
				 for(int k = 0; k < 8; k++)
				 {
					 for(int l = 0; l < 8; l++)
					 {
						 newSquare = squares[k][l];
						 Coordinates newCoordinate = new Coordinates(k, l);
						 
						 int cordinateRowDiff = newCoordinate.getRow() - currentCoordinate.getRow();
						 int cordinateColDiff = newCoordinate.getColumn() - currentCoordinate.getColumn();						 
						 int coordinateRowChange = 0;
						 int coordinateColChange = 0;
						 
						 if (cordinateRowDiff > 0) {
							 coordinateRowChange = 1;
						 } else {
							 coordinateRowChange = -1;
						 }
						 
						 if (cordinateColDiff > 0) {
							 coordinateColChange = 1;
						 } else {
							 coordinateColChange = -1;
						 }
						
						 //Force jump for kings.
						 /*if (currentSquare.getIcon().toString().equals("blackKing.gif") ||
								 currentSquare.getIcon().toString().equals("redKing.gif")) {
								 int rows_change = i - k;
								 int cols_change = j - l;
								 
								 // Everything inside is in trajectory of current king
								 if (rows_change == cols_change && oppositeSides(currentCoordinate,newCoordinate)) {
									 //if (checkSquare()) {
									//	 return true;
									 //}
									 distance = rows_change;
								 }
						 }*/
						 
						 // Force jump for non-kings. The two pieces are neighbors and one can jump to the other. 
						 // Also there is a piece in each one
						 if( Math.abs(i-k) == 1 &&  Math.abs(j-l) == 1 && checkSquare(currentCoordinate)
								 && checkSquare(newCoordinate) && oppositeSides(currentCoordinate,newCoordinate)) {
							 						 
							 if (k+coordinateRowChange > 7 || k+coordinateRowChange < 0) {
								 continue;
							 } else if (l+coordinateColChange > 7 || l+coordinateColChange < 0) {
								 continue;
							 }
							 
							 // Check that piece is moving in proper direction (not backward if not king)
							 // Check that jump spot is empty
							 // If above conditions satisfied, a jump can be made so should be forced
							 // needs to be changed for kings
							 if ((whichPlayersTurn == 1 && coordinateRowChange > 0) &&
									 !checkSquare(new Coordinates(k+coordinateRowChange, l+coordinateColChange))) {
								 //System.out.println("Row:" + (k+coordinateRowChange));
								 //System.out.println("Column:" + (l+coordinateColChange));
								 return true;
							 } else if ((whichPlayersTurn == 2 && coordinateRowChange < 0) &&
								!checkSquare(new Coordinates(k+coordinateRowChange, l+coordinateColChange))) {
								 //System.out.println("Row:" + (k+coordinateRowChange));
								 //System.out.println("Column:" + (l+coordinateColChange));
								 return true;
							 }
						 }

						 /* 
						 if( Math.abs(i-k) == 1 &&  Math.abs(j-l) == 1 && checkSquare(currentCoordinate)
								 && checkSquare(newCoordinate) && oppositeSides(currentCoordinate,newCoordinate)) {
							 return true;
						 }*/

					 }
				 }
			 }
		 }
				 		 
				 
		 return false;
	 }
	
	 // Returns true if the indicated coordinate has a current player's piece (based on turn). False otherwise.
	 public boolean isCurPlayersSquare(Coordinates c1)
	 {
		 if(!checkSquare(c1)) {
			 return false;
		 } else if (isP1Turn
				 && (squares[c1.getRow()][c1.getColumn()].getIcon()
						 .toString().equals("blackPiece.gif")
						 || squares[c1.getRow()][c1.getColumn()].getIcon()
						 .toString().equals("blackKing.gif")))  {
			 // Player 1/black player's turn
			 return true;
		 } else if (!isP1Turn
				 && (squares[c1.getRow()][c1.getColumn()].getIcon()
						 .toString().equals("redPiece.gif")
						 || squares[c1.getRow()][c1.getColumn()].getIcon()
						 .toString().equals("redKing.gif")))  {
			 // Player 1/red player's turn
			 return true;
		 }

		 return false;
	 }

	 // Saves the board
	 public void saveBoard()
	 {
		 // [TODO] Add saving of multi-jump, if multi-jump ever implemented

		 String filePath = "saved_game.txt";
		 try 
		 {
			 FileWriter fOutS = new FileWriter(filePath);
			 BufferedWriter out = new BufferedWriter(fOutS);

			 for(int i = 0; i < 8; i++) {
				 for(int j = 0; j < 8; j++) {
					 // row, column, color, king
					 // nn , nn    , 1 = black and 2 = red, 1 = king
					 Coordinates currentPiece = new Coordinates(i, j);

					 // Write out squares that have a piece
					 if(checkSquare(currentPiece)) 
					 {
						 String end_string = "";
						 if (squares[i][j].getIcon().toString().equals("blackPiece.gif")) {
							 end_string = "1,0";
						 } else if (squares[i][j].getIcon().toString().equals("blackKing.gif")) {
							 end_string = "1,1";
						 } else if (squares[i][j].getIcon().toString().equals("redPiece.gif")) {
							 end_string = "2,0";
						 } else if (squares[i][j].getIcon().toString().equals("redKing.gif")) {
							 end_string = "2,1";
						 }
						 out.write(i + "," + j + "," + end_string + "\n");
					 }
				 }
			 }
			 
			 // Get already elapsed time 
			 String elapsed_string = String.valueOf(myGui.elapsedTimeInSec());
			 
			 // Write out who is last player and already elapsed time
			 if (isP1Turn) {
				 out.write("1," + elapsed_string);
			 } else {
				 out.write("2," + elapsed_string);
			 }

			 out.close();
			 fOutS.close();
		 } 
		 catch (Exception e)
		 {
			 // Catch exception
		 }
	 }
	
	 // Loads the board
	 public void loadBoard(JTextField txtTurn) {
		ImageIcon blackPiece = new ImageIcon("blackPiece.gif");
		ImageIcon redPiece = new ImageIcon("redPiece.gif");
		 
		 // Remove previous pieces
		 for(int i = 0; i < 8; i++) {
			 for(int j = 0; j < 8; j++) {
				 removePiece(i,j);
			 }
		 }
		 
		 // Add loaded pieces
		 try {   
			 File savedGameFile = new File("saved_game.txt");
			 FileReader fr = new FileReader(savedGameFile);
			 BufferedReader reader = new BufferedReader(fr);

			 String currentLine = "";
			 while ((currentLine = reader.readLine()) != null) {
				 // row, column, color, king
				 // nn , nn    , 1 = black and 2 = red, 1 = king
				 String[] pieceParams = currentLine.split(",");
				 ImageIcon pieceColor = blackPiece;
				 boolean isBlack = true;
				 
				 // Skip steps below if reading last line of file (the ones that only have one value)
				 if (pieceParams.length == 2) {
					 if(Integer.parseInt(pieceParams[0]) == 1) {
						 isP1Turn = true;
						 txtTurn.setText("Player 1's Turn (Black)");
					 } else {
						 isP1Turn = false;
						 txtTurn.setText("Player 2's Turn (Red)");
					 }
					 
					 myGui.setTimer(1000*Integer.parseInt(pieceParams[1]));
					 break;
				 }
				 			 
				 if (Integer.parseInt(pieceParams[2]) == 2) {
					 pieceColor = redPiece;
					 isBlack = false;
				 } 
				 
				 Coordinates pieceCoordinates = new Coordinates(Integer.parseInt(pieceParams[0]), 
						 Integer.parseInt(pieceParams[1]));
				 placePiece(pieceColor, pieceCoordinates.getRow(), pieceCoordinates.getColumn());

				 if (Integer.parseInt(pieceParams[3]) == 1) {
					myGui.kingPiece(pieceCoordinates, isBlack);
				 }
			 }
		 } catch (IOException ex) {
			 // handle exception...
		 }
	 }
	 
	/**
	 * Kings a piece if necessary
	 * 
	 * @param king
	 *            the coordinates to test
	 */
	private void kingPiece(Coordinates king) 
	{
		if (isP1Turn
				&& squares[king.getRow()][king.getColumn()].getIcon()
						.toString().equals("blackPiece.gif")) 
		{
			if (king.getRow() == 7) 
			{
				myGui.kingPiece(king, true);
				System.out.println("King P1");
			}
		}

		else if (!isP1Turn
				&& squares[king.getRow()][king.getColumn()].getIcon()
						.toString().equals("redPiece.gif")) 
		{
			if (king.getRow() == 0) 
			{
				myGui.kingPiece(king, false);
				System.out.println("King P2");
			}
		}
	}

	/**
	 * Creates the squares and adds them to the board
	 */
	private void initializeBoard() 
	{
		// boolean to decide what color the square is
		boolean isBlack = true;

		// loops through the columns and rows
		for (int i = 0; i <= numCols - 1; i++) 
		{
			// changes color so two touching rows don't start with same color
			isBlack = !isBlack;
			for (int j = 0; j <= numRows - 1; j++) 
			{
				// creates new square and adds it to the board
				if (isBlack == true) 
				{
					SquarePanel nextSquare = new SquarePanel(i, j);
					// JButton nextSquare = new JButton(new ImageIcon());
					nextSquare.setBackground(Color.BLACK);
					nextSquare.setPreferredSize(new Dimension(62, 62));
					nextSquare.addMouseListener(pListener);
					squares[i][j] = nextSquare;
					add(nextSquare);
					isBlack = false;
				} 
				else 
				{
					SquarePanel nextSquare = new SquarePanel(i, j);
					// JButton nextSquare = new JButton();
					nextSquare.setBackground(Color.PINK);
					nextSquare.setPreferredSize(new Dimension(62, 62));
					nextSquare.addMouseListener(pListener);
					squares[i][j] = nextSquare;
					add(nextSquare);
					isBlack = true;
				}
			}
		}
	}

	public JButton getButton(int x, int y) 
	{
		return squares[x][y];
	}

	/**
	 * Put a piece on specific location on the board
	 * 
	 * @param image
	 *            a specific checker piece
	 * @param x
	 *            x location piece will be placed
	 * @param y
	 *            y location piece will be placed
	 */
	public void placePiece(Icon image, int x, int y) 
	{
		squares[x][y].setIcon(image);
	}

	/**
	 * Removes piece from specific location on the board
	 * 
	 * @param x
	 *            x-location piece will be placed
	 * @param y
	 *            y-location piece will be placed
	 */
	public void removePiece(int x, int y) 
	{
		squares[x][y].setIcon(null);
	}

	/**
	 * Makes sure that the coordinate selected is a piece
	 * 
	 * @param c1
	 *            the coordinate
	 * @return true if there is a piece
	 */
	public boolean checkSquare(Coordinates c1) 
	{
		if (squares[c1.getRow()][c1.getColumn()].getIcon() != null) 
		{
			return true;
		}
		return false;
	}

	/**
	 * Returns who's turn it is back to the GUI so that it can be displayed
	 * 
	 * @return The current players turn
	 */
	public boolean getWhoTurn() 
	{
		return isP1Turn;
	}

}